package com.example.rabbit.config;

import cn.hutool.core.util.EnumUtil;
import com.example.rabbit.BeanUtils;
import com.example.rabbit.enums.DelayEnum;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * topic交换机
 */
@Configuration
@DependsOn({"beanUtils"})
public class DelayConfig {

    private static final Map<String, Object> args = new HashMap<>();

    private static final String TYPE = "x-delayed-message";

    static {
        args.put("x-delayed-type", "direct");
    }


    @PostConstruct
    public void customExchange() {
        CustomExchange customExchange;
        Queue queue;
        LinkedHashMap<String, DelayEnum> map = EnumUtil.getEnumMap(DelayEnum.class);
        for (Map.Entry<String, DelayEnum> entry : map.entrySet()) {
            customExchange = new CustomExchange(entry.getValue().getExchange(), TYPE, true, false, args);
            queue = new Queue(entry.getValue().getQueue());
            BeanUtils.registerBean(entry.getValue().getExchange(), customExchange);
            BeanUtils.registerBean(entry.getValue().getQueue(), queue);
            BeanUtils.registerBean(entry.getKey(), BindingBuilder.bind(queue).to(customExchange).with(entry.getValue().getRoutingKey()).noargs());
        }
    }
}
